package structure.DecoratorPattern.introduce.demo;

public class DecoratorPattern {
    public static void main(String[] args) {
        System.out.println("装饰之前");
        Shape circle = new Circle();
        circle.draw();

        System.out.println("装饰之后");
        Shape decoratorCircle = new RedShapeDecorator(new Circle());
        decoratorCircle.draw();

        Shape decoratorRectangle = new RedShapeDecorator(new Rectangle());
        decoratorRectangle.draw();
    }
}

/**
 * 被装饰者
 * */
interface Shape{
    void draw();
}
/**
 * 被装饰者实现类 Circle
 * */
class Circle implements Shape{
    public void draw() {
        System.out.println(this.getClass().getSimpleName() + "draw" );
    }
}
/**
 * 被装饰者实现类 Rectangle
 * */
class Rectangle implements Shape{

    public void draw() {
        System.out.println(this.getClass().getSimpleName() + "draw" );
    }
}

/**
 * 被装饰者抽象类
 * */
abstract class ShapeDecorator implements Shape{
    protected Shape shape;//protected 本类，同包类，子类可以访问
    public ShapeDecorator(Shape shape){
        this.shape = shape;
    }
    public void draw() {
        System.out.println(shape.getClass().getSimpleName() + "draw" );
    }
}
/**
 * 被抽象者实现类，其装饰逻辑在此实现
 * */
class RedShapeDecorator extends ShapeDecorator{

    public RedShapeDecorator(Shape shape) {
        super(shape);
    }
    @Override
    public void draw() {
        System.out.println(shape.getClass().getSimpleName() + "draw" );
        setRedBorder(this.shape);
    }

    private void setRedBorder(Shape shape){
        System.out.println(shape.getClass().getSimpleName() + "Border Color: Red");
    }
}